import java.util.List;
import java.util.Date;

public class Manager extends User {
    public Manager(String id, String username, String password, String phone) {
        super(id, username, password, phone, "经理");
    }

    @Override
    public boolean login(String password) {
        if (locked) {
            System.out.println("账户已锁定");
            return false;
        }
        if (this.password.equals(password)) {
            loginAttempts = 0;
            return true;
        } else {
            loginAttempts++;
            if (loginAttempts >= 5) {
                locked = true;
                System.out.println("密码错误次数过多，账户已锁定");
            }
            return false;
        }
    }

    public void addMovie(Movie movie, List<Movie> movies) {
        movies.add(movie);
        Log.record("添加影片", "添加影片[" + movie.getName() + "]", this.username);
    }

    public void addShow(Show show, List<Show> shows) {
        Date now = new Date();
        Date maxDate = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000L);
        if (show.getShowTime().before(now) || show.getShowTime().after(maxDate)) {
            throw new IllegalArgumentException("只能排未来7天内的场次");
        }
        shows.add(show);
        Log.record("添加场次", "添加影片[" + show.getMovie().getName() + "]场次", this.username);
    }

    public void updateShow(Show show, List<Show> shows) {
        Show existing = shows.stream().filter(s -> s.getShowId().equals(show.getShowId())).findFirst().orElse(null);
        if (existing == null) {
            throw new IllegalArgumentException("场次不存在");
        }
        if (existing.getTickets().size() > 0) {
            throw new IllegalArgumentException("该场次已有售票，不能修改");
        }
        shows.remove(existing);
        shows.add(show);
        Log.record("修改场次", "修改场次[" + show.getShowId() + "]", this.username);
    }
}